# This file is part of Notehaus.

# Notehaus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Notehaus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Notehaus.  If not, see <https://www.gnu.org/licenses/>.
open(FH, '<', shift);
open(OFH, '>', shift);
@LIST = (<FH>);
foreach (@LIST) {
@LIST2 = ($_);
for ($i=0; $i<=$#LIST2; $i++) {
$_ = $LIST2[$i];
next if $_ !~ /\)(?!')/;
$pos2 = $+[0];
@LIST2 = (@LIST2[0 .. $i-1], @LIST2[$i+1 .. $#LIST2]);
while(/(?<!')\(/g) {
	$pos1 = $-[0];
	$pos_ = $+[0];
	while(/(\)(?!'))|((?<!')\|(?!'))/g) {
		die "Malformed parenthesis expression: $_" if $2 eq '' and $1 eq '';
		@LIST2 = (@LIST2[0 .. $i-1], $_, @LIST2[$i .. $#LIST2]);
        	$LIST2[$i] = substr ($_, 0, $pos1) . substr ($_, $pos_, $-[0]-$pos_) . substr ($_, $pos2);
		last if $1 ne '';
		$pos_ = $+[0];
	}
}
}
  print OFH @LIST2;
}
close FH;
close OFH;
